<?php

namespace App\Listeners\CommentCreated;

use App\Events\CommentCreated;
use App\Models\Score;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class AssignScoreToUser
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CommentCreated  $event
     * @return void
     */
    public function handle(CommentCreated $event)
    {
        $comment = $event->comment;
        $info =  ['original' => $comment->toArray()];
        $info['action_title'] = $comment->detail_excerpt;
        $info['action_id'] = $comment->id;
        if ($comment->pid) {
            //回复的目标，为评论
            $parentComment=  $comment->parentComment;
            $info['target_id'] = $parentComment->id;
            $info['target_title'] = $parentComment->detail_excerpt;
            $info['target_username'] = $parentComment->user->username;
        } else {
            //回复的目标，为帖子
            $topic = $comment->topic;
            $info['target_id'] = $topic->id;
            $info['target_title'] = $topic->titleExcerpt;
            $info['target_username'] = $topic->user->username;
        }
        Score::assignForCreateComment($comment->uid, null, null, $info);

        logger(sprintf(
            "%s, assign score to user: %s, info: %s",
            __METHOD__, $comment->uid, json_encode($info, 336)
        ));
    }
}
